package com.shiguo365.market.display.activity;


import android.content.DialogInterface;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.shiguo365.market.R;
import com.shiguo365.market.application.MyApplication;
import com.shiguo365.market.display.adapter.OrderDetailAdapter;
import com.shiguo365.market.display.base.BaseActivity;
import com.shiguo365.market.display.listener.NavigationFinishClickListener;
import com.shiguo365.market.display.util.RefreshUtils;
import com.shiguo365.market.display.util.ToastUtils;
import com.shiguo365.market.display.view.IOrderDetailView;
import com.shiguo365.market.presenter.contract.IOrderDetailPresenter;
import com.shiguo365.market.presenter.implement.OrderDetailPresenter;
import com.shiguo365.market.utils.HandlerUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class OrderDetailActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener,IOrderDetailView {

    private String TAG="OrderDetailActivity";

    private LinearLayoutManager mLayoutManager;

    OrderDetailAdapter mAdapter;

    private IOrderDetailPresenter mOrderDetailPresenter;

    @BindView(R.id.buttonOrderCancel) Button mBtnOrderCancel;

    @BindView(R.id.textViewAddress) TextView mTvAddress;

    @BindView(R.id.textViewAddress2) TextView mTvAddress2;

    @BindView(R.id.textViewOrderId) TextView mTvOrderId;

    @BindView(R.id.textViewOrderTime) TextView mTvOrderTime;

    @BindView(R.id.textViewOrderAmount) TextView mTvOrderAmount;

    @BindView(R.id.textViewOrderStatus) TextView mTvOrderStatus;


    private List<Object> jsonObjectList= new ArrayList<>();;

    @BindView(R.id.order_detail_refresh_layout) protected SwipeRefreshLayout refreshLayout;
    @BindView(R.id.my_recycler_view_confirm) protected RecyclerView mRecyclerView;
    @BindView(R.id.order_detail_list_toolbar) protected Toolbar toolbar;


    protected String orderid = "";



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d(TAG,"test1:onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_detail);

        ButterKnife.bind(this); //ButterKnife初始化

        Intent intent =getIntent();
        orderid=intent.getStringExtra("oid");

        mOrderDetailPresenter=new OrderDetailPresenter(this,this);
        toolbar.setNavigationOnClickListener(new NavigationFinishClickListener(this));
        Log.d(TAG,"test3:"+jsonObjectList);
        mAdapter = new OrderDetailAdapter(mOrderDetailPresenter,jsonObjectList);
        mRecyclerView.setAdapter(mAdapter);
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);
//如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        mRecyclerView.setHasFixedSize(true);
//创建并设置Adapter
        RefreshUtils.initOnCreate(refreshLayout, this);
        RefreshUtils.refreshOnCreate(refreshLayout, this);

//        mBtnOrderDetail.bringToFront();
    }

    @Override
    public void onRefresh() {
        Log.d(TAG,"test2:onRefresh");
        //添加刷新代码
        jsonObjectList.clear();


        mOrderDetailPresenter.getOrderDetail(orderid);

    }

    @Override
    public void OnGetOrderDetailOK(JSONArray jsonarray) {

        for (int i = 0; i < jsonarray.size(); i++) {
            this.jsonObjectList.add(jsonarray.get(i));
        }

        mAdapter.notifyDataSetChanged();

        Log.d(TAG,"OnGetOrderDetailOK:"+jsonObjectList.toString());
    }

    @Override
    public void OnGetInfoOK(JSONObject result) {

//        Long timestamp = Long.parseLong(timestampString)*1000;
//        String date = new java.text.SimpleDateFormat(formats).format(new java.util.Date(timestamp));

        JSONObject order = result.getJSONObject("order");
        JSONObject address = result.getJSONObject("address");

        String order_id = order.getString("orderid");

        String createAt = order.getString("createAt");

        Long timestamp = Long.parseLong(order_id);
        Log.d(TAG,"timestamp====>"+timestamp);
        String date = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date(timestamp));
        Log.d(TAG,"date====>"+date);
        mTvOrderTime.setText(date);

        int status = order.getIntValue("status");
        String amount = order.getString("amount");

        String status_text = "";
        if(status==1000){
            status_text = "待受理";
        }else if(status==2000){
            status_text = "待完成";
        }else if(status==3000){
            status_text = "已完成";
        }else if(status==4000){
            status_text = "平台撤消";
        }else if(status==-1){
            status_text = "用户撤消";
        }

        if(status==1000){
            mBtnOrderCancel.setVisibility(View.VISIBLE);
        }else{
            mBtnOrderCancel.setVisibility(View.GONE);
        }

        mTvOrderId.setText(order_id);
        mTvOrderStatus.setText(status_text);
        mTvOrderAmount.setText(amount);


        String name = address.getString("toName");
        String phone = address.getString("toPhone");
        String street = address.getString("toStreet");

        String str = name+" ("+phone+")";


        mTvAddress.setText(str);
        mTvAddress2.setText(street);
    }

    @Override
    public void OnGetOrderDetailFinsh() {
        refreshLayout.setRefreshing(false);
    }


    @OnClick(R.id.buttonOrderCancel)
    public void cancelOrder(){

        AlertDialog.Builder builder=new AlertDialog.Builder(this);  //先得到构造器
        builder.setTitle("订单撤消确认"); //设置标题
        builder.setMessage("您确定要撤消订单吗?"); //设置内容
        builder.setIcon(R.mipmap.ic_launcher);//设置图标，图片id即可
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { //设置确定按钮
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                mOrderDetailPresenter.cancelOrder(orderid,"-1");

            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { //设置取消按钮
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();

            }
        });


        //参数都设置完成了，创建并显示出来
        builder.create().show();

    }

    //显示消息
    @Override
    public void showOrderDetailMessages(String msg) {
        ToastUtils.with(this).show(msg);
    }

    //创建订单成功后进入订单列表页面
    @Override
    public void orderDetailSuccess() {
        Intent intent = new Intent(OrderDetailActivity.this,OrderActivity.class);
        startActivity(intent);
        finish();
    }

}
